using System.Collections.Generic;
using UFrame.MapJson;
using UnityEditor;
using UnityEngine;

namespace UFrame
{
    /// <summary>
    /// TriggerEventEditor
    /// </summary>
    public class TriggerEventEditor : MapEditorBase
    {
        public enum ETriggerType
        {
            None = 0,

            /// <summary>
            /// 光墙
            /// </summary>
            LightWall,

            /// <summary>
            /// 怪物刷新
            /// </summary>
            ShowMonster,

            /// <summary>
            /// npc刷新
            /// </summary>
            ShowNpc,

            /// <summary>
            /// 踩点区域
            /// </summary>         
            Area,

            /// <summary>
            /// 剧情
            /// </summary>
            Cg,

            /// <summary>
            /// npc刷新
            /// </summary>
            Talk,

            /// <summary>
            /// 触发机关
            /// </summary>
            Gear,

            /// <summary>
            /// 寻路指引
            /// </summary>
            WayGuide,

            /// <summary>
            /// 特效
            /// </summary>
            Effect,

            /// <summary>
            /// 采集物刷新
            /// </summary>
            ShowCollect,
        }

        /// <summary>
        /// 此类的一个全局对象
        /// </summary>
        private static TriggerEventEditor windows = null;

        private string[] itemTypeString =
            {"空事件", "光墙  ", "刷怪物", "刷Npc", "区域  ", "Cg   ", "Talk ", "机关  ", "指引  ", "特效  ", "刷采集物"};

        private ETriggerType curTriggerType = ETriggerType.LightWall;


        private static string lightWallPath = "Assets/Res/Effect/Prefab/Scene/Prefab/FX_scene_fuwenmen.prefab";

        private static string wayGuidePath =
            "Assets/Res/Effect/Prefab/Role/qinshi/FX_xinshouyindao_guangquan_04.prefab";

        /// <summary>
        /// 滚动位置
        /// </summary>
        public Vector2 rollPosition = Vector2.zero;

        /// <summary>
        /// 数据
        /// </summary>
        List<TriggerEventInfo> data = new List<TriggerEventInfo>();

        /// <summary>
        /// 移出表数据索引
        /// </summary>
        List<int> index = new List<int>();

        public TriggerEventEditor()
        {
        }

        /// <summary>
        /// 打开
        /// </summary>
        public static void OpenWindow()
        {
            if (windows == null)
            {
                windows = GetWindowWithRect<TriggerEventEditor>(new Rect(0, 0, 1200, 320), false, "事件触发",
                    true);
                windows.InitWindow();
                windows.Show();
            }
        }

        /// <summary>
        /// 初始化窗口
        /// </summary>
        public override void InitWindow()
        {
            //反序列化地图信息
            //XmlConfig.TriggerEventXmlConfig nxm = DataManagerInstance.instance.Get<XmlConfig.TriggerEventXmlConfig>();
            object nxm = null;
            if (nxm == null)
            {
                return;
            }

            data.Clear();
            //this.data = nxm.data;

            data.Sort(delegate (TriggerEventInfo x, TriggerEventInfo y) { return x.id.CompareTo(y.id); });

            for (int i = 0; i < data.Count; ++i)
            {
                switch ((ETriggerType)data[i].type)
                {
                    case ETriggerType.LightWall:
                        //光墙                      
                        GameObject tmp = AssetDatabase.LoadAssetAtPath(lightWallPath, typeof(GameObject)) as GameObject;
                        data[i].go = Instantiate(tmp);
                        data[i].go.name = data[i].id.ToString();
                        data[i].go.transform.localPosition = data[i].position;
                        data[i].go.transform.eulerAngles = data[i].rot;
                        data[i].go.transform.localScale = data[i].scale;
                        break;
                    case ETriggerType.WayGuide:
                        GameObject tmpguile =
                            AssetDatabase.LoadAssetAtPath(wayGuidePath, typeof(GameObject)) as GameObject;
                        data[i].go = Instantiate(tmpguile);
                        data[i].go.name = data[i].id.ToString();
                        data[i].go.transform.localPosition = data[i].position;
                        data[i].go.transform.eulerAngles = data[i].rot;
                        data[i].go.transform.localScale = data[i].scale;
                        break;


                    case ETriggerType.ShowMonster:
                    case ETriggerType.ShowNpc:
                    case ETriggerType.ShowCollect:

                        data[i].go = GameObject.CreatePrimitive(PrimitiveType.Sphere);
                        data[i].go.name = data[i].id.ToString();
                        data[i].go.transform.localPosition = data[i].position;
                        break;
                    case ETriggerType.Area:
                        data[i].go = GameObject.CreatePrimitive(PrimitiveType.Cube);
                        data[i].go.name = data[i].id.ToString();
                        data[i].go.transform.localPosition = data[i].position;
                        data[i].go.transform.eulerAngles = data[i].rot;
                        data[i].go.transform.localScale = data[i].scale;
                        break;

                    case ETriggerType.None:
                    case ETriggerType.Cg:
                    case ETriggerType.Talk:
                    case ETriggerType.Gear:
                    case ETriggerType.Effect:

                        data[i].go = GameObject.CreatePrimitive(PrimitiveType.Sphere);
                        data[i].go.name = data[i].id.ToString();
                        data[i].go.transform.localPosition = data[i].position;
                        data[i].go.transform.eulerAngles = data[i].rot;
                        data[i].go.transform.localScale = data[i].scale;
                        break;
                }
            }

            index = new List<int>();
        }

        /// <summary>
        /// 窗口关闭时调用
        /// </summary>
        public void OnDestroy()
        {
            Destroy();
        }

        private void Destroy()
        {
            for (int i = 0; i < data.Count; ++i)
            {
                DestroyImmediate(data[i].go);
            }

            index.Clear();
        }

        /// <summary>
        /// 关闭
        /// </summary>
        public static void CloseWindow()
        {
            if (windows)
            {
                //销毁对象
                windows.Destroy();
                //关闭窗口
                windows.Close();
            }
        }


        /// <summary>
        /// GUI
        /// </summary>
        private void OnGUI()
        {
            GUI.backgroundColor = Color.gray;
            GUILayout.BeginArea(new Rect(position.width * 0.01f, position.height * 0.02f, position.width * 0.98f,
                position.height * 0.74f));

            rollPosition = GUILayout.BeginScrollView(rollPosition);
            index.Clear();

            for (int i = 0; i < data.Count; ++i)
            {
                GUILayout.BeginHorizontal();
                GUI.color = Color.white;
                GUILayout.Label("Id :");
                int id = EditorGUILayout.IntField(data[i].id);
                if (id != data[i].id)
                {
                    data[i].id = id;
                    if (data[i].go != null)
                    {
                        data[i].go.name = id.ToString();
                    }
                }

                GUILayout.Label("TriggerIds :");
                string triggerid = EditorGUILayout.TextField(data[i].triggerid);
                if (triggerid != data[i].triggerid)
                {
                    data[i].triggerid = triggerid;
                }

                GUILayout.Label("Param :");
                string eventparam = EditorGUILayout.TextField(data[i].param);
                if (eventparam != data[i].param)
                {
                    data[i].param = eventparam;
                }

                GUILayout.Label("延迟 :");
                data[i].delay = EditorGUILayout.IntField(data[i].delay);

                GUILayout.Label(itemTypeString[(int)data[i].type] + "类型 :");
                data[i].type = EditorGUILayout.FloatField(data[i].type);

                GUI.color = Color.yellow;
                GUILayout.Label("注释:");
                data[i].note = EditorGUILayout.TextField(data[i].note);

                GUI.color = Color.green;
                if (GUILayout.Button("select", GUILayout.Width(64)))
                {
                    Selection.activeGameObject = data[i].go;
                    SceneView.lastActiveSceneView.camera.transform.position =
                        Selection.activeGameObject.transform.position;
                    SceneView.lastActiveSceneView.AlignViewToObject(Selection.activeGameObject.transform);
                }

                GUI.color = Color.red;
                if (GUILayout.Button("delete", GUILayout.Width(64)))
                {
                    index.Add(i);
                }

                GUILayout.EndHorizontal();
            }

            GUILayout.EndScrollView();
            GUILayout.EndArea();

            for (int i = 0; i < index.Count; ++i)
            {
                DestroyImmediate(data[index[i]].go);
                data.RemoveAt(index[i]);
            }

            GUI.color = Color.green;

            GUILayout.BeginArea(new Rect(position.width * 0.01f, position.height * 0.78f, position.width * 0.98f,
                position.height * 0.18f));

            GUILayout.BeginHorizontal();
            for (int i = 0; i < itemTypeString.Length; i++)
            {
                if (GUILayout.Button(itemTypeString[i], GUILayout.Height(30)))
                {
                    curTriggerType = (ETriggerType)i;
                    TriggerEventInfo trigger = new TriggerEventInfo();
                    trigger.type = (int)curTriggerType;
                    switch (curTriggerType)
                    {
                        case ETriggerType.LightWall:
                            GameObject tmp =
                                AssetDatabase.LoadAssetAtPath(lightWallPath, typeof(GameObject)) as GameObject;
                            trigger.go = Instantiate(tmp);
                            trigger.go.name = trigger.id.ToString();
                            data.Add(trigger);
                            break;
                        case ETriggerType.WayGuide:
                            GameObject tmpguide =
                                AssetDatabase.LoadAssetAtPath(wayGuidePath, typeof(GameObject)) as GameObject;
                            trigger.go = Instantiate(tmpguide);
                            trigger.go.name = trigger.id.ToString();
                            data.Add(trigger);
                            break;
                        case ETriggerType.Area:
                            trigger.go = GameObject.CreatePrimitive(PrimitiveType.Cube);
                            trigger.go.name = trigger.id.ToString();
                            data.Add(trigger);
                            break;
                        case ETriggerType.None:
                        case ETriggerType.Cg:
                        case ETriggerType.Talk:
                        case ETriggerType.ShowMonster:
                        case ETriggerType.ShowNpc:
                        case ETriggerType.ShowCollect:
                        case ETriggerType.Gear:
                        case ETriggerType.Effect:

                            trigger.go = GameObject.CreatePrimitive(PrimitiveType.Sphere);
                            trigger.go.name = trigger.id.ToString();
                            data.Add(trigger);
                            break;
                    }
                }
            }

            GUILayout.EndArea();
            GUILayout.EndHorizontal();
            if (GUI.Button(
                new Rect(position.width * 0.01f, position.height * 0.88f, position.width * 0.98f,
                    position.height * 0.1f), "保存"))
            {
                Save();
            }

            GUI.color = Color.gray;
        }

        /// <summary>
        /// 保存数据
        /// </summary>
        public override void Save()
        {
            for (int i = 0; i < data.Count; ++i)
            {
                if (data[i].go != null)
                {
                    if (data[i].go != null)
                    {
                        data[i].position = data[i].go.transform.localPosition;
                        data[i].scale = data[i].go.transform.localScale;
                        data[i].rot = data[i].go.transform.eulerAngles;
                    }
                }
            }

            isChange = false;
        }
    }
}